﻿using UnityEngine;
using QFramework;
using QAssetBundle;

namespace ProjectSurvivor
{
    public abstract class PowerUp : GamePlayObject
    {
        public bool FlyingToPlayer {set; get;}
		
        private int mFlyingToPlayerFrameCount = 0;
        
        private void Update()
        {
            if (FlyingToPlayer)
            {
                if (mFlyingToPlayerFrameCount == 0)
                {
                    GetComponent<SpriteRenderer>().sortingOrder = 5;
                }

                mFlyingToPlayerFrameCount++;

                if (Player.Default)
                {
                    var direction = Player.Default.Direction2DFrom(this);
                    var distance = direction.magnitude;

                    if (mFlyingToPlayerFrameCount <= 15)
                    {
                        transform.Translate(Time.deltaTime * -2f * direction);
                    }
                    else
                    {
                        transform.Translate(Time.deltaTime * 7.5f * direction);
                    }

                    if (distance < 0.5f)
                    {
                        Execute();
                    }
                }
            }
        }
        
        public abstract void Execute();
    }
}